package cn.tedu.egweibo.controller;

import cn.tedu.egweibo.base.respont.JsonResult;
import cn.tedu.egweibo.base.respont.StatusCode;
import cn.tedu.egweibo.mapper.UserMapper;
import cn.tedu.egweibo.pojo.dto.UserLoginParam;
import cn.tedu.egweibo.pojo.dto.UserRegParam;
import cn.tedu.egweibo.pojo.entity.User;
import cn.tedu.egweibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@RestController
@Api(tags = "01.用户管理模块")
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.注册功能
     */
    @PostMapping("reg")
    @ApiOperation("注册功能")
    @ApiOperationSupport(order = 10)
    public JsonResult reg(@Validated @RequestBody UserRegParam userRegParam) {
        log.debug("userRegParam = " + userRegParam);
        /*
            1.接收客户端传递的数据;
            2.校验数据的合法性[暂时不做,后期使用验证框架做];
            3.校验用户名是否被占用[SELECT COUNT(*) FROM user WHERE username=?]
              3.1 被占用: 返回响应;
              3.2 未被占用: 在用户表中插入1条数据,返回注册成功[INSERT INTO user VALUES(...)];
         */
        String username = userRegParam.getUsername();
        int i = userMapper.selectByUsername(username);
        if (i > 0) {//被占用
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        //未被占用
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreateTime(new Date());
        log.debug("user = " + user);
        userMapper.insert(user);

        return JsonResult.ok();
    }
    @PostMapping("login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 200)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam,
                            @ApiIgnore HttpSession session){
        log.debug("userLoginParam = " + userLoginParam);
        UserVO userVO = userMapper.selectByUsernameAAndPassword(userLoginParam);
        if(userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return JsonResult.ok(userVO);
    }
    @GetMapping("cerrentUser")
    @ApiOperation("用户查询")
    @ApiOperationSupport(order = 300)
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.debug("userVO = " + userVO);
        if(userVO ==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        return JsonResult.ok(userVO);
    }
    @GetMapping("logout")
    @ApiOperation("用户退出")
    @ApiOperationSupport(order = 400)
    public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }
}
